﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using System.Threading;

namespace BJHZRZ.User
{

    public enum SqlErrorCommand{
        ReTry,
        GiveUp
    }
    public delegate void TimeOutEnvent(String sql,out SqlErrorCommand s);



    public class DbConnect
    {

        private System.Data.SqlClient.SqlConnection DbConnection;
        public event TimeOutEnvent TimeOutEvent;
        private SqlConnectionStringBuilder stringBuilder;





        public DbConnect(String connectionString)
        {

            stringBuilder = new SqlConnectionStringBuilder(connectionString);
               DbConnection = new SqlConnection(stringBuilder.ToString());
            DbConnection.Open();
            DbConnection.InfoMessage += new SqlInfoMessageEventHandler(DbConnection_InfoMessage);
        }

        void DbConnection_InfoMessage(object sender, SqlInfoMessageEventArgs e)
        {
            
        }

        public int ExecuteSql(String sql)
        {
            SqlCommand execute= new SqlCommand(sql, DbConnection);
            return execute.ExecuteNonQuery();
        }

        public void Dispose()
        {
            DbConnection.Dispose();

        }

        public SqlDataReader ExecuteSqlwithRe(String sql)
        {
            SqlCommand execute= new SqlCommand(sql, DbConnection);
            return execute.ExecuteReader();
        }

        /// <summary>
        /// 获取strSql对应的SqlDataAdapter
        /// </summary>
        /// <param name="strSql">SQL语句</param>
        /// <returns>SqlDataAdapter</returns>
        public SqlDataAdapter GetDataAdapter(string strSql)
        {
            SqlCommand command = new SqlCommand(strSql, DbConnection);
            SqlDataAdapter da = new SqlDataAdapter(command);
            return da;
        }

        /// <summary>
        /// 获取和strSql对应的DataSet
        /// </summary>
        /// <param name="strSql">SQL语句</param>
        /// <returns>DataSet</returns>
        public DataSet GetDataSet(string strSql)
        {
         
            SqlDataAdapter da = GetDataAdapter(strSql);
            DataSet ds = new DataSet();
            try
            {
                da.Fill(ds);
            }
            catch (SqlException ex)
            {
                 if (ex.Class == 11 || ex.Class ==20)
                {
                    if (this.TimeOutEvent != null)
                    {
                        SqlErrorCommand returnCommand;
                        this.TimeOutEvent(strSql, out returnCommand);
                        if (returnCommand == SqlErrorCommand.ReTry)
                        {
                            this.stringBuilder.ConnectTimeout += 2000;
                            
                            this.DbConnection.Dispose();
                            this.DbConnection = new SqlConnection(this.stringBuilder.ToString());
                             this.DbConnection.Open();
                            
                            return GetDataSet(strSql);
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                   this.Err(ex);
            }         
            return ds;
        }
        /// <summary>
        /// 数据库有关的错误处理
        /// </summary>
        /// <param name="ex">异常</param>
        protected void Err(Exception ex)
        {
            if (ex is SqlException)
            {
                SqlException Ex = (SqlException)ex;
                  MessageBox.Show(new Form(), "Class:" + Ex.Class + "ex.Message:" + Ex.Message + "ex.Number" + Ex.Number.ToString() + "ex.Soure" + Ex.Server + Ex.Source + Ex.State + "other" + Ex.ToString());
            }
            else
            {
                MessageBox.Show(new Form(), ex.ToString());
               }
        }

      //  public SqlR

        public void sqlgo(List<String> sql)
        {
            SqlTransaction sqltransaction = this.DbConnection.BeginTransaction();

            foreach (String Sql1 in sql)
            {
                SqlCommand sqlcommand = new SqlCommand(Sql1,this.DbConnection, sqltransaction);
                sqlcommand.ExecuteNonQuery();

            }
            sqltransaction.Commit();
        }
    }
}
